<?php $this->pageHeader($this->member->getFullName()); ?>

<?php if (!isset($this->userId)) : ?>
           <a href="<?php echo $this->url(array('controller' => 'user', 'action' => 'edit'), null, true); ?>" class="button">
               <img src="<?php echo $this->imageLink('/images/icons/pencil.png'); ?>"  alt="Rediger profil" title="Rediger profil" />
               Rediger profil
           </a>
            <a href="<?php echo $this->url(array('controller' => 'user', 'action' => 'change-password'), null, true); ?>" class="button">
                <img src="<?php echo $this->imageLink('/images/icons/user_change_password.png') ?>" alt="Endre passord" title="Endre passord" />
                Endre passord
            </a>
<?php endif; ?>

<p class="clear" />

<?php echo $this->flashMessage()->renderMessages(); ?>

<?php
    if($this->isPermanentPassive) {
        echo '<h2>Permanent passiv</h2>';
        echo $this->flashMessage()->renderMessage('Satt som permanent passiv', RedCross_View_Helper_FlashMessage::INFO);
      echo '<p>&nbsp;</p>';
    }
?>


<div class="profile_box personalia">
<h2>Personalia</h2>
<table class="profile">
    <tr>
        <td class="label">Adresse</td>
        <td class="content"><?php echo $this->escape($this->member['address']); ?></td>
    </tr>
    <?php if (!is_null($this->member['address_2'])): ?>
    <tr>
        <td class="label"></td>
        <td class="content">
          <?php echo $this->escape($this->member['address_2']); ?>
        </td>
    </tr>
    <?php endif; ?>
    <tr>
        <td class="label">Postnummer</td>
        <td class="content"><?php echo $this->escape($this->member->getPostalCode()); ?></td>
    </tr>
    <tr>
        <td class="label">Poststed</td>
        <td class="content"><?php echo $this->escape($this->member->getPostalPlace()); ?></td>
    </tr>
    <tr>
        <td class="label">Medlemsnummer</td>
        <td class="content"><?php echo $this->escape($this->member->redcross_id); ?></td>
    </tr>

<?php if (count($this->member['Phonenumbers']) > 0) : ?>
  <?php $i = 0; ?>
  <?php foreach ($this->member['Phonenumbers'] as $number) : ?>
    <tr>
      <td class="label">
      <?php if ($i == 0) { ?>
      <?php echo "Telefonnummer"; ?>
      <?php } ?>
      </td>
      <td class="content"><?php echo $this->escape($number['number']); ?></td>
    </tr>
    <?php $i++; ?>
  <?php endforeach; ?>
<?php endif; ?>

<?php if (count($this->member['emails']) > 0) : ?>
  <?php $i = 0; ?>
  <?php foreach ($this->member['emails'] as $email) : ?>
    <tr>
      <td class="label">
      <?php if ($i == 0) { ?>
      <?php echo "Epost"; ?>
      <?php } ?>
      </td>
      <td class="content"><?php echo $this->escape($email['address']); if ($email['is_main']) { echo '*'; } ?></td>
    </tr>
    <?php $i++; ?>
  <?php endforeach; ?>
<?php endif; ?>

</table>
</div>

<div class="profile_box right_float">

<div class="profile_box user">
<h2>Bruker</h2>
<table class="profile">
    <tr>
        <td class="label">Brukernavn</td>
        <td class="content"><?php echo $this->escape($this->user->username); ?></td>
    </tr>
    <tr>
        <td class="label">Brukergrupper</td>
        <td class="content">
        <?php
            $groups = '';
            foreach ($this->user->Groups as $group) {
                $groups .= $group['name'] . ', ';
            }
            echo $this->escape(substr($groups, 0, -2));
        ?>
        </td>
    </tr>
</table>
</div>

<div class="profile_box next_of_kin">
<h2>Pårørende</h2>
<?php if (count($this->member->NextOfKin) > 0): ?>
<ol>
    <?php foreach($this->member->NextOfKin as $NextOfKin) { ?>
    <li>
    <?php echo $this->escape($NextOfKin->first_name . ' ' . $NextOfKin->last_name); ?></td>
    (<?php echo $this->escape($NextOfKin->phone_number); ?>)
    </li>
    <?php } ?>
</ol>
<?php else: ?>
    <p>Ingen p&aring;r&oslash;rende registrert</p>
<?php endif; ?>
</div>

</div>

<?php if ($this->isAdmin): ?>
<p class="clear"></p>
<div class="profile_box comment">
<h2>Kommentar</h2>
<?php echo str_replace("\n", "<br />", $this->member['comment']); ?>
</div>
<?php endif; ?>

<p class="clear"></p>

<div class="profile_box future_shifts">
<h2>Kommende vakter</h2>
<?php if (count($this->futureShifts) > 0) : ?>
    <table>
        <thead>
            <tr>
                <th>Vakt</th>
                <th>Fra</th>
                <th>Til</th>
                <th>Arrangement</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($this->futureShifts as $shift) : ?>
            <tr>
                <td>
                    <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shift['id']), null, true); ?>">
                        <?php echo $this->escape($shift['name']); ?>
                    </a>
                </td>
                <?php
                    $fromDate = new RedCross_Date();
                    $fromDate->import($shift['from_date']);

                    $toDate = new RedCross_Date();
                    $toDate->import($shift['to_date']);
                ?>
                <td><?php echo $this->escape($fromDate->getDateTimeStr()); ?></td>
                <td><?php echo $this->escape($toDate->getDateTimeStr()); ?></td>
                <td>
                    <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'event', 'action' => 'view', 'id' => $shift['Event']['id']), null, true); ?>">
                        <?php echo $this->escape($shift['Event']['name']); ?>
                    </a>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php else : ?>
  Ingen kommende vakter
<?php endif; ?>
</div>

<div class="profile_box last_shifts">
<h2>Siste vakter</h2>
<?php if (count($this->pastShifts) > 0) : ?>
    <table class="profile">
        <thead>
            <tr>
                <th class="label">Vakt</th>
                <th class="label">Fra</th>
                <th class="label">Til</th>
                <th class="label">Arrangement</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($this->pastShifts as $shift) : ?>
            <tr>
                <td>
                    <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'shift', 'action' => 'view', 'id' => $shift['id']), null, true); ?>">
                        <?php echo $this->escape($shift['name']); ?>
                    </a>
                </td>
                <?php
                    $fromDate = new RedCross_Date();
                    $fromDate->import($shift['from_date']);

                    $toDate = new RedCross_Date();
                    $toDate->import($shift['to_date']);
                ?>
                <td><?php echo $this->escape($fromDate->getDateTimeStr()); ?></td>
                <td><?php echo $this->escape($toDate->getDateTimeStr()); ?></td>
                <td>
                    <a href="<?php echo $this->url(array('module' => 'core', 'controller' => 'event', 'action' => 'view', 'id' => $shift['Event']['id']), null, true); ?>">
                        <?php echo $this->escape($shift['Event']['name']); ?>
                    </a>
                </td>
            </tr>
    <?php endforeach; ?>
        </tbody>
    </table>
<?php else : ?>
Ingen tidligere vakter
<?php endif; ?>
</div>

<div class="profile_box">
<?php if (count($this->interests) > 0) : ?>
<h2>Arrangementer <?php echo $this->escape($this->member['first_name']); ?> har vist interesse for</h2>
<table>
    <thead>
        <tr>
            <th class="label">Arrangement</th>
            <th class="label">Fra</th>
            <th class="label">Til</th>
        </tr>
    </thead>
    <tbody>
        <?php foreach($this->interests as $interest) : ?>
        <tr>
            <td>
                <a href="<?php echo $this->url(array('module' => 'admin', 'controller' => 'event', 'action' => 'view', 'id' => $interest['Event']['id']), null, true); ?>">
                    <?php echo $this->escape($interest['Event']['name']); ?>
                </a>
            </td>
            <?php
                $fromDate = new RedCross_Date();
                $fromDate->import($interest['Event']['from_date']);

                $toDate = new RedCross_Date();
                $toDate->import($interest['Event']['to_date']);
            ?>
            <td><?php echo $this->escape($fromDate->getDateTimeStr()); ?></td>
            <td><?php echo $this->escape($toDate->getDateTimeStr()); ?></td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>
<?php endif; ?>
</div>

<div class="profile_box courses">
<h2>Kurs</h2>
<?php if (count($this->courses) > 0) : ?>
    <table class="profile">
        <thead>
            <tr>
                <th class="label">Kurs</th>
                <th class="label">Lagt til</th>
                <th class="label">Gyldig til</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($this->courses as $course) : ?>
            <tr>
                <td><?php echo $this->escape($course['name']); ?></td>
                <?php
                    $fromDate = new RedCross_Date();
                    $fromDate->import($course['MemberCourse']['0']['added_date']);

                    if ($course['MemberCourse']['0']['expires_date'] != null) {
                        $toDate = new RedCross_Date();
                        $toDate->import($course['MemberCourse']['0']['expires_date']);
                        $toDate = $this->escape($toDate->getDateTimeStr());
                    } else {
                        $toDate = '&infin;';
                    }
                ?>
                <td><?php echo $this->escape($fromDate->getDateTimeStr()); ?></td>
                <td><?php echo $toDate; ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php else : ?>
    <p>Ingen registrerte kurs</p>
<?php endif; ?>
</div>

<div class="profile_box competences">
<h2>Kompetanser</h2>
<?php if (count($this->competences) > 0) : ?>
    <table class="profile">
        <thead>
            <tr>
                <th class="label">Kompetanse</th>
                <th class="label">Lagt til</th>
                <th class="label">Gyldig til</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($this->competences as $competence) : ?>
            <tr>
                <td><?php echo $this->escape($competence['name']); ?></td>
                <?php
                    $fromDate = new RedCross_Date();
                    $fromDate->import($competence['MemberCourse']['0']['added_date']);

                    if ($competence['MemberCourse']['0']['expires_date'] != null) {
                        $toDate = new RedCross_Date();
                        $toDate->import($competence['MemberCourse']['0']['expires_date']);
                        $toDate = $this->escape($toDate->getDateTimeStr());
                    } else {
                        $toDate = '&infin;';
                    }
                ?>
                <td><?php echo $this->escape($fromDate->getDateTimeStr()); ?></td>
                <td><?php echo $toDate; ?></td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php else : ?>
    <p>Ingen registrerte kompetanser</p>
<?php endif; ?>
</div>

<div class="profile_box diplomas">
<h2>Sertifikater</h2>
<?php if (sizeof($this->diplomas) > 0): ?>
    <table>
        <thead>
            <tr>
                <th>Beskrivelse</th>
                <th>Tidspunkt</th>
                <th>&nbsp;</th>
            </tr>
        </thead>
        <tbody>
        <?php foreach($this->diplomas as $diploma): ?>
            <?php
                $class = "";
                if (preg_match('/.*\.(jpg|jpeg|png|gif)/i', $diploma->file_name) > 0):
                    $class = "image";
                endif;
            ?>
            <tr>
                <td><?php echo $diploma->description; ?></td>
                <td><?php echo $diploma->upload_date; ?></td>
                <td>
                <a href="<?php echo $this->imageLink('uploads/'.$diploma->file_name); ?>" class="<?php echo $class; ?>">Vis</a>
                </td>
            </tr>
        <?php endforeach; ?>
        </tbody>
    </table>
<?php else: ?>
    <p>Ingen sertifikater lastet opp</p>
<?php endif; ?>
</div>

<div>
    <?php if (count($this->passiveMessages) > 0): ?>
    <?php endif; ?>
</div>

<div class="profile_box passive_messages">
<h2>Passivmeldinger</h2>
<?php if (count($this->passiveMessages) > 0) : ?>
    <table class="profile">
        <thead>
            <tr>
                <th class="label">Meldt passiv i f&oslash;lgende perioder</th>
            </tr>
        </thead>
        <tbody>
        <?php
        foreach($this->passiveMessages as $passiveMessage):
            $startDate = false;
            $endDate = false;

            $date = new RedCross_Date($passiveMessage['date']);
            $currentDate = new RedCross_Date($passiveMessage['date']);
            if (!isset($previousDate)) $previousDate = new RedCross_Date($passiveMessage['date']);

            if (!$date->sub(1, Zend_Date::DAY)->equals($previousDate, Zend_Date::DAY)) {
                $startDate = $currentDate;

                if (!$currentDate->equals($previousDate)) {
                    $endDate = $previousDate;
                }
            }

            if ($endDate)   echo ' - '  .$endDate->getDateStr() . '</td></tr>';
            if ($startDate) echo '<tr><td>' . $startDate->getDateStr();

            $previousDate = new RedCross_Date($passiveMessage['date']);
        endforeach;

        echo ' - ' . $previousDate->getDateStr() . '</td></tr>';
        ?>
        </tbody>
    </table>
<?php else : ?>
    <p>Ingen passivmeldinger</p>
<?php endif; ?>
</div>
